﻿using BLL;
using models;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace yuejilalala
{
    public partial class shopcart : System.Web.UI.Page
    {
        CartMarager marager = new CartMarager();
        OrderManager marager2 = new OrderManager();
        PayManager manager3 = new PayManager();
        public Cart CartSum =new Cart();
       
        public List<Cart> CartSumByid = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            CartSum.ProPrice = 0;
            userDl();
            if (Session["sessid"]==null)
            {
                Response.Redirect("Login.aspx");
            }
            HiddenField1.Value = Session["sessid"].ToString();
            if (!IsPostBack)
            {
                //登陆显示购物车内容总数
                J_MiniCartNum.Text = marager.CartCount((Session["sessid"].ToString())).ProPrice.ToString();//显示出购物车内物品.数量
                //购物车初始显示数据
                List<Cart> list = marager.SelCart(HiddenField1.Value); 
                Repeater1.DataSource = list;
                Repeater1.DataBind();
                CartSum = marager.SumCart(HiddenField1.Value);






                //截取商品规格
                //string ProSpeci = marager.SelCartSpeci("1", (list[0].Proid).ToString())[0].ProSpeci;
                //string[] ProSpecis= ProSpeci.Split('/');



            }


        }
        public void jiazai() 
        {
            
            List<Cart> list = marager.SelCart(HiddenField1.Value);
            Repeater1.DataSource = list;
            Repeater1.DataBind();
            if (marager.SumCart(HiddenField1.Value) != null)
            {
                CartSum = marager.SumCart(HiddenField1.Value);
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            if (marager.CartCount(HiddenField1.Value).ProPrice==0)
            {
                Response.Write("<script>alert('购物车为空 请添加商品')</script>");
                return;
            }
            dingdan();
            //jiazai();

        }
        //将购物车中的东西生成个订单
        public  void dingdan() 
        {
            string ddh= DateTime.Now.Year +""+ DateTime.Now.Month+""+ DateTime.Now.Day+""+DateTime.Now.Second.ToString() +""+ ((DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Second) * 3).ToString();//简单的订单号生成
           //点击结算 生成订单 
                Order orders = new Order();      
                orders.Orders = ddh;
                orders.DateNow = DateTime.Now;//当前时间
                orders.UserID =int.Parse(HiddenField1.Value);//暂时固定用户
                orders.State = 1;//1为未支付
                marager2.InsertOrders(orders);
            //将购物车中的物品放入购买信息详情中
            Pay pay = new Pay();
            List<Cart> cList = marager.SelCart(HiddenField1.Value);
            foreach (var item in cList)
            {
                pay.Userid = item.Userid;//用户ID
                pay.Storeid = item.Storeid;//店铺ID
                pay.Proid = item.Proid;//商品ID
                pay.Count = item.Count;//数量
                pay.Sum=item.Ssum;//单价
                pay.Batch =orders.Orders.ToString();//订单号
                manager3.add(pay);
            }
            Session["ddh"] = ddh;//将商品的订单号放入session中
            //然后清空购物车（暂时注掉）
            marager.DelCartSum(HiddenField1.Value);
            //跳转到结算界面
            Response.Redirect("ShopPay.aspx");

        }

        protected void Repeater1_ItemCommand1(object source, RepeaterCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();
            if (e.CommandName=="jia")
            {
                //点击进入加的
                //调用增加数量的操作
                marager.AddSubCartCount(id, "+");
               
            }
            if (e.CommandName=="jian")
            {
                //点击进入减
                //调用减少数量的操作
                marager.AddSubCartCount(id, "-");
            }
            if (e.CommandName=="del")
            {
                //调用删除
                marager.DelCart(id);
            }
            jiazai();


        }
        public void userDl()
        {
            if (Session["sess"] != null)
            {
                //用户存在则显示出来 并且隐藏注册和登陆显示
                User user = Session["sess"] as User;
                hl_dlm.Text = "用户名:" + user.UName;
                hl_dlm.Visible = true;//显示登陆名控件
                hl_dl.Visible = false;//请登陆隐藏
                hl_zc.Visible = false;//注册隐藏
            }

        }
    }
}